import { defineStore } from 'pinia'
import request from '@/utils/request'
import { ApiRes } from '@/types/data'
import { GoodsInfo, SkuType } from '@/types/goods'
export default defineStore('goods', {
  state() {
    return {
      // 商品详情
      goodsInfo: {} as GoodsInfo,
    }
  },
  actions: {
    // 获取商品详情的方法
    async getGoodsInfo(id: string) {
      const res = await request.get<ApiRes<GoodsInfo>>('/goods', {
        params: { id },
      })
      this.goodsInfo = res.data.result
    },
    // 重置商品信息
    resetGoodsInfo() {
      this.goodsInfo = {} as GoodsInfo
    },
  },
  getters: {
    // 过滤有效的skus
    filterSkus(): SkuType[] {
      return this.goodsInfo.skus.filter((sku) => sku.inventory > 0)
    },
  },
})
